package com.iut2.projet2013;

import java.io.File;
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends FragmentActivity implements
		AdapterView.OnItemSelectedListener, ViewPager.OnPageChangeListener {

	private ViewPager viewPager;
	private Gallery gallery;
	private ArrayList<String> galleryFileList;
	private TextView textView;
	private Handler handler;

	/**
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		galleryFileList = new ArrayList<String>();
		File file = new File(Environment.getExternalStorageDirectory(),
				"Pictures");

		File[] files = file.listFiles();
		for (File picture : files) {
			if (!picture.isDirectory()) {
				galleryFileList.add(picture.getPath());
			}
		}

		if (galleryFileList.size() <= 0) {
			Toast.makeText(this, "Aucune images dans le répertoire courant !", Toast.LENGTH_LONG).show();
		}

		viewPager = (ViewPager) findViewById(R.id.view_pager);
		viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
		viewPager.setOnPageChangeListener(this);

		gallery = (Gallery) findViewById(R.id.gallery);
		gallery.setAdapter(new ImageAdapter());
		gallery.setOnItemSelectedListener(this);

		textView = (TextView) findViewById(R.id.title);

		handler = new Handler();
		
		/* BORDEL ANTOINE - TEST CREATION BDD */
		Bdd DB = new Bdd();
		
		Boolean test = DB.onCreate();
		Toast.makeText(getBaseContext(), test.toString(), Toast.LENGTH_SHORT).show();
		/* FIN BORDEL ANTOINE */

	}

	// gallery item selected
	public void onItemSelected(AdapterView<?> adapterView, View view, int i,
			long l) {
		updateUI(i);
	}

	// view pager item selected
	public void onPageSelected(int i) {
		updateUI(i);
	}

	private void updateUI(final int i) {
		handler.post(new Runnable() {
			public void run() {
				gallery.setSelection(i);
				viewPager.setCurrentItem(i);
				//textView.setText("Photo #" + i);
				textView.setText("Annote it");
			}
		});
	}

	private Bitmap decodeSampledBitmapFromUri(String path, int reqWidth,
			int reqHeight) {
		Bitmap bm = null;

		// First decode with inJustDecodeBounds=true to check dimensions
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(path, options);

		// Calculate inSampleSize
		options.inSampleSize = calculateInSampleSize(options, reqWidth,
				reqHeight);

		// Decode bitmap with inSampleSize set
		options.inJustDecodeBounds = false;
		bm = BitmapFactory.decodeFile(path, options);

		return bm;
	}

	private int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth, int reqHeight) {
		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;

		if (height > reqHeight || width > reqWidth) {
			if (width > height) {
				inSampleSize = Math.round((float) height / (float) reqHeight);
			} else {
				inSampleSize = Math.round((float) width / (float) reqWidth);
			}
		}

		return inSampleSize;
	}

	public void onNothingSelected(AdapterView<?> adapterView) {
	}

	public void onPageScrolled(int i, float v, int i1) {
	}

	public void onPageScrollStateChanged(int i) {
	}

	public class ImageAdapter extends BaseAdapter {

		public int getCount() {
			return galleryFileList.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			Bitmap bm = decodeSampledBitmapFromUri(
					galleryFileList.get(position), 150, 100);

			ImageView imageView = new ImageView(Main.this);
			imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			imageView.setImageBitmap(bm);

			return imageView;
		}
	}

	public class MyAdapter extends FragmentPagerAdapter {
		public MyAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public int getCount() {
			return galleryFileList.size();
		}

		@SuppressLint("ValidFragment")
		@Override
		public Fragment getItem(final int position) {
			return new Fragment() {
				@Override
				public View onCreateView(LayoutInflater inflater,
						ViewGroup container, Bundle savedInstanceState) {

					final Bitmap bm = decodeSampledBitmapFromUri(
							galleryFileList.get(position), 200, 200);

					ImageView imageView = new ImageView(Main.this);
					imageView.setImageBitmap(bm);

					imageView.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View arg0) {
							// Start new Activity
							Intent intent = new Intent(Main.this, ShowImage.class);
							Bundle b = new Bundle();
							b.putParcelable("Bitmap", bm);
							intent.putExtras(b); //Put your id to your next Intent
							startActivity(intent);
						}
					});

					return imageView;
				}
			};
		}
	}
}